package cn.net.cfss.fgbp.gateway.util;

import cn.net.cfss.fgbp.base.common.GlobalConstants;
import cn.net.cfss.fgbp.base.vo.ReturnVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;

public class ReturnUtils {

    public static Mono<Void> returnErr(ServerHttpResponse originalResponse,ReturnVo returnVo )
    {
        return returnErr(originalResponse,returnVo.getPlain().getResultCode(),returnVo.getPlain().getResultMsg());
    }

    public static Mono<Void> returnErr(ServerHttpResponse originalResponse,String code,String msg )
    {
        originalResponse.setStatusCode(HttpStatus.OK);
        originalResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        JSONObject plainJson = new JSONObject();
        plainJson.put(GlobalConstants.PARAMS_RETURN_CODE, code);
        plainJson.put(GlobalConstants.PARAMS_RETURN_MSG, msg);
        JSONObject packet= new JSONObject();
        packet.put("plain",plainJson);
        byte[] bytes = packet.toString().getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = originalResponse.bufferFactory().wrap(bytes);
        return originalResponse.writeWith(Flux.just(buffer));
    }


}
